package com.shxc.smarttrade.index.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.currency.adapter.ViewPagerFragmentAdapter;
import com.shxc.smarttrade.index.fragment.IndexMessageFragment;
import com.shxc.smarttrade.index.fragment.IndexNoticeFragment;
import com.shxc.smarttrade.index.model.MessageHavingModel;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;
import com.shxc.smarttrade.utils.UiUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 消息中心
 */
@ContentView(R.layout.activity_message_center)
public class MessageCenterActivity extends BaseActivity {

    @ViewInject(R.id.title_textview)
    TextView title_textview;
    @ViewInject(R.id.left_imageview)
    ImageView left_imageview;
    @ViewInject(R.id.right_textview)
    TextView right_textview;
    @ViewInject(R.id.point_textview)
    TextView point_textview;
    @ViewInject(R.id.tablayout)
    TabLayout tablayout;
    @ViewInject(R.id.viewpager)
    ViewPager viewpager;

    List<Fragment> fragmentList;
    IndexMessageFragment indexMessageFragment;

    private static final String[] tabName = {"官方公告", "系统消息"};
    private List<String> tabTitleList = Arrays.asList(tabName);

    private boolean isOprate = false;

    @Event({R.id.left_imageview,R.id.right_textview})
    private void onClick(View view) {

        switch (view.getId()) {
            case R.id.left_imageview:
                finish();
                break;
            case R.id.right_textview:
                //编辑
                isOprate = !isOprate;
                setRight();
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        title_textview.setText("消息中心");
        right_textview.setText("编辑");
        right_textview.setVisibility(View.GONE);

        left_imageview.setVisibility(View.VISIBLE);

        indexMessageFragment = new IndexMessageFragment();

        fragmentList = new ArrayList<>();
        fragmentList.add(new IndexNoticeFragment());
        fragmentList.add(indexMessageFragment);

        viewpager.setOffscreenPageLimit(2);
        viewpager.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList, tabTitleList));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {
                    right_textview.setVisibility(View.GONE);
                    isOprate = false;
                    setRight();
                } else {
                    right_textview.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tablayout.setupWithViewPager(viewpager);
        tablayout.post(new Runnable() {
            @Override
            public void run() {
                UiUtils.setTabLineWidth(tablayout,60);//字线同宽
            }
        });
        msghavemsg();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 5:
                    MessageHavingModel messageHavingModel = (MessageHavingModel) msg.obj;
                    if (messageHavingModel.getErr_code().equals("0")) {
                        if (messageHavingModel.getData().isHaveMsg()) {
                            point_textview.setVisibility(View.VISIBLE);
                            if (messageHavingModel.getData().getMsgNum()<100) {
                                point_textview.setText(messageHavingModel.getData().getMsgNum()+"");
                            }else{
                                point_textview.setText("99+");
                            }

                        } else {
                            point_textview.setVisibility(View.GONE);
                        }
                    }
                    break;
            }
            super.dispatchMessage(msg);
        }
    };

    //设置右边文字
    public void setRight() {
        if (isOprate) {
            right_textview.setText("取消");
        }else{
            right_textview.setText("编辑");
        }
        indexMessageFragment.setOprate(isOprate);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (isOprate) {
                    isOprate = false;
                    setRight();
                    return true;
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    //是否还有未读消息
    public void msghavemsg() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", spImp.getUserId() + "");

        MyOkHttpUtils.getServiceDatas(map, Contents.MSG_HAVEMSG, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, MessageHavingModel.class);
                    if (jam.isSuccess()) {
                        MessageHavingModel model = (MessageHavingModel) jam.getData();
                        Message msg = new Message();
                        msg.what = 5;
                        msg.obj = model;
                        handler.sendMessage(msg);
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }

    public void setOprate(boolean oprate) {
        isOprate = oprate;
    }

    public void setRightText(int state) {
        right_textview.setVisibility(state);
    }
}
